﻿using PaddleOCRSharp;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace PdfOperator
{
    public partial class FrmOCR : Form
    {
        public FrmOCR()
        {
            InitializeComponent();
        }

        private void FrmOCR_Load(object sender, EventArgs e)
        {
            this.picBox.AllowDrop = true;
        }

        // 图片路径
        string imgPath = null;

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Filter = "(*.jpg,*.png,*.jpeg,*.bmp)|*.jpg;*.png;*.jpeg;*.bmp;|All files(*.*)|*.*";
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                imgPath = openFile.FileName;
                FileStream file = new FileStream(imgPath, FileMode.Open);

                // this.picBox.Image = Image.FromFile(openFile.FileName);
                this.picBox.Image = Image.FromStream(file);
                file.Close();

                lblCopyRes.Visible = false;
            }
        }

        /// <summary>
        /// 点击按钮开始识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            // PaddleOCRSharp文字识别
            if (this.picBox.Image == null || string.IsNullOrEmpty(imgPath))
            {
                MessageBox.Show("请选择图片！", "提示");
                return;
            }
            // 读取图片数据
            var imagebyte = File.ReadAllBytes(imgPath);
            // 创建位图
            Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte));
            // OCR配置
            OCRModelConfig config = null;
            // OCR参数
            OCRParameter oCRParameter = new OCRParameter();
            // 识别结果
            OCRResult ocrResult = new OCRResult();
            // 创建引擎开始识别
            using (PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter))
            {
                try
                {
                    ocrResult = engine.DetectText(bitmap);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            // 输出结果到编辑框
            txtResult.Text = ocrResult.Text;
        }


        /// <summary>
        /// 复制结果到剪贴板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCopyResult_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(txtResult.Text, true);
            lblCopyRes.Visible = true;
        }

        /// <summary>
        /// 开始拖放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.picBox.Cursor = Cursors.Arrow; //指定鼠标形状   
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        /// <summary>
        /// 接收拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picBox_DragDrop(object sender, DragEventArgs e)
        {
            var paths = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetEnumerator();
            while (paths.MoveNext())
            {
                string file = paths.Current.ToString();
                string ext = Path.GetExtension(file).ToLower();
                if (ext == ".png" || ext == "jpg" || ext == "jpeg" || ext == "bmp")
                {
                    imgPath = file;
                    this.picBox.Image = Image.FromFile(file);
                }
            }

            this.picBox.Cursor = Cursors.Default; //还原鼠标形状 
        }

        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
